package lz.controls
{
	import com.common.utils.DisplayObjectUtil;
	
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;

	/**
	 * 按钮
	 * @author lxh
	 */	
	public class LButton extends EventDispatcher
	{
		/**
		 * 按钮皮肤
		 */		
		private var _view 			:MovieClip;
		/**
		 * 是否可交互
		 */		
		protected var _enable		:Boolean = false;
		/**
		 * 正常状态
		 */		
		public static const UP		:int = 1;
		/**
		 * 移上状态
		 */		
		public static const OVER	:int = 2;
		/**
		 * 选中状态
		 */		
		public static const SELECT:int = 3;
		/**
		 * 灰掉状态
		 */		
		public static const GRAY	:int = 4;
		/**
		 *组件坐标 
		 */		
		private var _x :Number;
		private var _y :Number;
		/**
		 *数据 
		 */		
		public var data :*;
		
		public function LButton(view :MovieClip)
		{
			_view = view;
			_view.gotoAndStop(UP);
		}
		/**
		 *获取视图对象 
		 * @return 
		 * 
		 */		
		public function get view():MovieClip
		{
			return _view;
		}
		/**
		 *按钮名字 
		 * @return 
		 * 
		 */		
		public function get name():String
		{
			return _view.name;
		}
		/**
		 * 设置 x
		 * @param value
		 * 
		 */		
		public function set x(value :Number):void
		{
			_x = value;
			_view.x = _x;
		}
		/**
		 * 获取 x
		 * @return 
		 * 
		 */		
		public function get x():Number
		{
			return _x;
		}
		/**
		 *设置 y 
		 * @param value
		 * 
		 */		
		public function set y(value :Number):void
		{
			_y = value;
			_view.y = _y;
		}
		/**
		 *获取 y 
		 * @return 
		 * 
		 */		
		public function get y():Number
		{
			return _y;
		}
		/**
		 *获取宽度 
		 * @return 
		 * 
		 */		
		public function get width():Number
		{
			return _view.width;
		}
		/**
		 *获取高度 
		 * @return 
		 * 
		 */		
		public function get height():Number
		{
			return _view.height;
		}
		/**
		 *设置状态 
		 * @param value
		 * 
		 */		
		public function setStatus(value :int):void
		{
			_view.gotoAndStop(value);
		}
		/**
		 *获取状态 
		 * @return 
		 * 
		 */		
		public function getStatus():int
		{
			return _view.currentFrame;
		}
		/**
		 *获取交互状态 
		 * @return 
		 * 
		 */		
		public function get enable() : Boolean
		{
			return _enable;
		}
		/**
		 *设置交互状态 
		 * @param value
		 * 
		 */		
		public function set enable(value:Boolean) : void
		{
			if (_enable == value)
			{
				return;
			}
			_enable = value;
			if (_enable)
			{
				addEvent();
				//_view.mouseChildren = true;
				_view.mouseEnabled = true;
			}
			else
			{
				removeEvent();
				_view.mouseChildren = false;
				//_view.mouseEnabled = false;
			}
		}
		/**
		 *获取可见度 
		 * @return 
		 * 
		 */		
		public function get visible():Boolean
		{
			return _view.visible;
		}
		/**
		 *设置可见性 
		 * @param value
		 * 
		 */		
		public function set visible(value:Boolean):void
		{
			_view.visible = value;
		}
		/**
		 *获取按钮鼠标状态 
		 * @return 
		 * 
		 */		
		public function get buttonMode():Boolean
		{
			return _view.buttonMode;
		}
		/**
		 * 设置按钮鼠标状态
		 * @param value
		 * 
		 */		
		public function set buttonMode(value:Boolean):void
		{
			_view.buttonMode = value;
		}
		/**
		 *设置子节点鼠标交互 
		 * @param value
		 * 
		 */		
		public function set mouseChildren(value :Boolean):void
		{
			_view.mouseChildren = value;
		}
		/**
		 *获取节点鼠标交互状态 
		 * @return 
		 * 
		 */		
		public function get mouseChildren():Boolean
		{
			return _view.mouseChildren;
		}
		/**
		 *设置鼠标交互 
		 * @param value
		 * 
		 */		
		public function set mouseEnabled(value :Boolean):void
		{
			_view.mouseEnabled = value;
		}
		/**
		 *获取鼠标交互状态 
		 * @return 
		 * 
		 */		
		public function get mouseEnabled():Boolean
		{
			return _view.mouseEnabled;
		}
		/**
		 *按钮设置滤镜 
		 * @param $value
		 * 
		 */
		public function set filter($value:Boolean):void
		{
			if($value)
			{
				var arr:Array = [new ColorMatrixFilter([0.5,0.5,0.5,0,0,
														0.5,0.5,0.5,0,0,
														0.5,0.5,0.5,0,0,
														0,0,0,1,0,])];
				_view.filters = arr;
			}else
			{
				_view.filters = null;
			}
		}
		/**
		 *添加事件 
		 * 
		 */		
		private function addEvent() : void
		{
			_view.addEventListener(MouseEvent.CLICK, onClick);
			_view.addEventListener(MouseEvent.ROLL_OVER, rollOver);
			_view.addEventListener(MouseEvent.ROLL_OUT, rollOut);
			_view.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			_view.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		/**
		 *删除事件 
		 */		
		private function removeEvent() : void
		{
			_view.removeEventListener(MouseEvent.CLICK, onClick);
			_view.removeEventListener(MouseEvent.ROLL_OVER, rollOver);
			_view.removeEventListener(MouseEvent.ROLL_OUT, rollOut);
			_view.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			_view.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		/**
		 *点击事件 
		 * @param event
		 * 
		 */		
		protected function onClick(event:MouseEvent) : void
		{
			dispatchEvent(event);
		}
		/**
		 *移上事件 
		 * @param event
		 * 
		 */		
		protected function rollOver(event:MouseEvent) : void
		{
			_view.gotoAndStop(OVER);
			dispatchEvent(event);
		}
		/**
		 *移出事件 
		 * @param event
		 * 
		 */		
		protected function rollOut(event:MouseEvent) : void
		{
			_view.gotoAndStop(UP);
			dispatchEvent(event);
		}
		/**
		 *按下事件 
		 * @param event
		 * 
		 */		
		protected function mouseDown(event:MouseEvent) : void
		{
			_view.gotoAndStop(SELECT);
			dispatchEvent(event);
		}
		/**
		 *松开事件 
		 * @param event
		 * 
		 */		
		protected function mouseUp(event:MouseEvent) : void
		{
			_view.gotoAndStop(OVER);
			dispatchEvent(event);
		}
		/**
		 *激活点击事件 
		 * 
		 */		
		public function activeClickEvent():void
		{
			_view.addEventListener(MouseEvent.CLICK, onClick);
			_view.addEventListener(MouseEvent.ROLL_OVER, rollOver);
			_view.mouseEnabled = true;
		}
	}
}